#define _USE_MATH_DEFINES
#include "show.h"
using namespace std;
//创建窗口并设置格式
void init_gragh()
{
    initgraph(win_width, win_height);
    setbkcolor(WHITE);
    setbkmode(TRANSPARENT); //文字背景模式为透明
    settextcolor(BLACK);
    settextstyle(letter_height, letter_width, _T("宋体"));
    setlinecolor(BLACK);
    cleardevice();
}
//清除左半边
void clean_left()
{
    clearrectangle(0, 0, static_cast<int>(win_width/2), win_height);
}
//清除右半边
void clean_right()
{
    clearrectangle(static_cast<int>(win_width/2), 0, win_width, win_height);
}

//功能    绘制三角形光标在地图上指示车站
//输入    pos station—站点在地图上中心坐标对
//        enum SELECT status—站点的选择状态（决定颜色）
void show_station(pos target_station, enum SELECT status)
{
    int triangle_size = 10;     //外接圆半径
    int move_bias = 32;         //竖直方向偏移，以向上为正方向
    setpolyfillmode(WINDING);
    switch (status){
    case START:
    	setfillcolor(0x0000FF); //亮红
    	break;
    case END:
        setfillcolor(0xFF0000); //亮蓝
        break;
    case FIND:
        setfillcolor(0x00FF00); //亮绿
        break;
    default:
        break;
    }

    POINT pts[3];
    double angle = M_PI / 2;
    for (int i = 0; i < 3; i++){
        pts[i].x = static_cast<int>(target_station.x - cos(angle) * triangle_size);
        pts[i].y = static_cast<int>(target_station.y - move_bias + sin(angle) * triangle_size);
        angle += M_PI * 2/3;
    }
    solidpolygon(pts, 3);
}

//功能    绘制下划线光标闪烁
/*输入    int enable--光标显示是否使能
          int& flash--光标闪烁计数器的引用
          int posi_i—光标左端的坐标在number_picture[]中的下标   */
void flash_line(bool enable, int& flash, int posi_i)
{
    if (enable){
        if (flash < flash_cycle / refresh_cycle / 2){ //显示下划线光标
            line(number_picture[posi_i].x, number_picture[posi_i].y + letter_height + 2,
                 number_picture[posi_i].x + letter_width, number_picture[posi_i].y + letter_height + 2);
        } else {                                      //擦除下划线光标
            clearrectangle(number_picture[posi_i].x, number_picture[posi_i].y + letter_height + 2,
                           number_picture[posi_i].x + letter_width, number_picture[posi_i].y + letter_height + 2);
        }
        flash++;
        flash = (flash < flash_cycle / refresh_cycle)? flash : 0;
    } else {
        clearrectangle(number_picture[posi_i].x, number_picture[posi_i].y + letter_height + 2,
                       number_picture[posi_i].x + letter_width, number_picture[posi_i].y + letter_height + 2);
    }
}
//在对应位置上绘制数字，将下划线光标移动到右边一格
/*输入    int& posi_i--光标左端的坐标在number_picture[]中的下标
          int number--显示的数字，或者标识符                       */
void chart_push(int& posi_i, int number)
{
    clearrectangle(number_picture[posi_i].x, number_picture[posi_i].y + letter_height + 2,
                   number_picture[posi_i].x + letter_width, number_picture[posi_i].y + letter_height + 2);
    if (number >= 0 && number <= 9){
        TCHAR str[2];
        _itot_s(number, str, 2, 10);
        outtextxy(number_picture[posi_i].x, number_picture[posi_i].y, str);
    } else {
        if (number != INT_MIN){
            cout << "错误：每次只能显示一位数字，\n";
        }   //INT_MIN被设置为相当于NULL
    }
    posi_i++;
}
//将下划线光标移动到左边一格，清除对应位置上的数字
void chart_back(int& posi_i)
{
    clearrectangle(number_picture[posi_i].x, number_picture[posi_i].y + letter_height + 2,
                   number_picture[posi_i].x + letter_width, number_picture[posi_i].y + letter_height + 2);
    posi_i--;
    clearrectangle(number_picture[posi_i].x, number_picture[posi_i].y,
                   number_picture[posi_i].x + letter_width, number_picture[posi_i].y + letter_height);
}
//输入框右边的错误显示和错误显示清除
/*输入    enum SELECT--被选中的输入框，决定了高度
          const TCHAR* warning--报错中文字符串的指针  */
void show_wrong(enum SELECT row, const TCHAR* warning)
{
    settextcolor(0x0000FF); //bright red
    int y = text_top;
    switch(row){
    case FIND:
    case START:
        break;
    case END:
        y += (letter_height + line_gap);
        break;
    case TICKET:
        y += (letter_height*3 + line_gap*3);
        break;
    default:
        break;
    }
    outtextxy(text_end_right + letter_width*5, y, warning);
    settextcolor(BLACK);
}
void clean_wrong(enum SELECT row)
{
    int y = text_top;
    switch(row){
    case FIND:
    case START:
        break;
    case END:
        y += (letter_height + line_gap);
        break;
    case TICKET:
        y += (letter_height*3 + line_gap*3);
        break;
    default:
        break;
    }
    //图省事按大了清
    clearrectangle(text_end_right + letter_width*5 -1, y-1, win_width, y + letter_height+1);
}

//更加安全的图片显示程序
/*输入    int x--左上角横坐标
          int y--左上角纵坐标
          int target_width--目标宽度
          int target_height--目标高度
          const char* path--文件路径*/
void show_image(int x, int y, int target_width, int target_height, const char* path)
{
    IMAGE img_src;
    loadimage(&img_src, _T(path), target_width, target_height, 1);
    if (img_src.getheight() > 0 && img_src.getwidth() > 0){
        if (x + target_width > win_width || y + target_height > win_height){
            cout << "警告：图片尺寸过大，显示不完全\n\n";
        }
        putimage(x, y, &img_src);
    } else {
        //用的相对路径，懒得换算成绝对路径输出了
        //用stdout而不是stderr，是因为后者不熟
        cout << "报错：加载失败，图片路径：" << path << "\n\n";
    }
}

//查询站点页面的文字显示
void show_word_search()
{
    outtextxy(text_end_right - textwidth(_T("查询车站")) - 4, text_top, _T("查询车站"));
}
//选点选票页面的文字显示
void show_word_buy()
{
    outtextxy(text_end_right - textwidth(_T("起点")) - 4, text_top, _T("起点"));
    outtextxy(text_end_right - textwidth(_T("终点")) - 4, text_top + letter_height + line_gap, _T("终点"));
    outtextxy(text_end_right - textwidth(_T("单价")) - 4, text_top + letter_height*2 + line_gap*2, _T("单价"));
    outtextxy(text_end_right - textwidth(_T("票数")) - 4, text_top + letter_height*3 + line_gap*3, _T("票数"));
}
//选点选票页面更新“单价”数值
void show_single_price(int single_price)
{
    TCHAR str[3];
    _itot_s(single_price, str, 3, 10);
    outtextxy(text_end_right + 2, text_top + letter_height*2 + line_gap*2, str);
}
//投币付款页面的文字显示，正因为有这么一堆参数我们才不合并为show_text()
void show_pay(int target, int present)
{
    TCHAR str1[4], str2[4];
    _itot_s(target, str1, 4, 10);
    _itot_s(present, str2, 4, 10);

//我们希望像现实中一样，数字以外的文字位置保持不变并留有空隙，否则早就用（宽）字符串运算一行搞定了
    clearrectangle(text_end_right, text_top, text_end_right + letter_width * 2, text_top + letter_height);
    outtextxy(text_end_right - letter_width*4 - 4, text_top, _T("总计"));
    outtextxy(text_end_right, text_top, str1);
    outtextxy(text_end_right + letter_height*3 + 4, text_top, _T("元"));
    outtextxy(text_end_right - letter_width*4 - 4, text_top + letter_height + line_gap , _T("已付"));
    outtextxy(text_end_right, text_top + letter_height + line_gap, str2);
    outtextxy(text_end_right + letter_height*3 + 4, text_top + letter_height + line_gap, _T("元"));
}
//完成购票页面的文字显示
void show_finish(const cash& return_cash,int number_ticket)
{
    TCHAR s1[3],s2[2],s3[2],s4[3];
    _itot_s(return_cash.number_10yuan, s1, 3, 10);
    _itot_s(return_cash.number_5yuan, s2, 2, 10);
    _itot_s(return_cash.number_1yuan, s3, 2, 10);
    _itot_s(number_ticket, s4, 3, 10);

    outtextxy(text_end_left - letter_width*4 - 4, text_top, _T("找回"));
    outtextxy(text_end_left, text_top, s1);
    outtextxy(text_end_left + letter_width*2 + 4, text_top, _T("张10元"));
    outtextxy(text_end_left, text_top + letter_height + line_gap, s2);
    outtextxy(text_end_left + letter_width*2 + 4, text_top + letter_height + line_gap, _T("张5元"));
    outtextxy(text_end_left, text_top + letter_height*2 + line_gap*2, s3);
    outtextxy(text_end_left + letter_width*2 + 4, text_top + letter_height*2 + line_gap*2, _T("张1元"));
    outtextxy(text_end_left - letter_width*2 - 4, text_top + letter_height*3 + line_gap*3, _T("出"));
    outtextxy(text_end_left, text_top + letter_height*3 + line_gap*3, s4);
    outtextxy(text_end_left + letter_width*2 + 4, text_top + letter_height*3 + line_gap*3, _T("张车票"));
}
//退出退币页面的文字显示
void show_giveback(const cash& return_cash)
{
    TCHAR s1[3],s2[2],s3[2];
    _itot_s(return_cash.number_10yuan, s1, 3, 10);
    _itot_s(return_cash.number_5yuan, s2, 2, 10);
    _itot_s(return_cash.number_1yuan, s3, 2, 10);

    outtextxy(text_end_left - letter_width*4 - 4, text_top, _T("退还"));
    outtextxy(text_end_left, text_top, s1);
    outtextxy(text_end_left + letter_width*2 + 4, text_top, _T("张10元"));
    outtextxy(text_end_left, text_top + letter_height + line_gap, s2);
    outtextxy(text_end_left + letter_width*2 + 4, text_top + letter_height + line_gap, _T("张5元"));
    outtextxy(text_end_left, text_top + letter_height*2 + line_gap*2, s3);
    outtextxy(text_end_left + letter_width*2 + 4, text_top + letter_height*2 + line_gap*2, _T("张1元"));
}
